﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MVC4App.SecurityServiceReference;

namespace MVC4App.Areas.Administrators.Models
{
    public static class Mapper
    {
        #region Site
        /// <summary>
        /// Froms the model.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns></returns>
        public static Site FromModel(this SiteDto obj)
        {
            if (obj == null) return null;
            return new Site
            {
                Id = obj.Id,
                Title = obj.Title,
                Description = obj.Description,
                Controller = obj.Controller,
                Order = obj.Order,
                ParentId = obj.ParentId,
                Status = obj.Status,
                IsGroup = obj.IsGroup
            };
        }
        /// <summary>
        /// To the model.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns></returns>
        public static SiteModel ToModel(this SiteDto obj)
        {
            if (obj == null) return null;
            return new SiteModel()
            {
                Id = obj.Id,
                Title = obj.Title,
                Description = obj.Description,
                Order = obj.Order,
                ParentId = obj.ParentId,
                Controller = obj.Controller,
                IsGroup = obj.IsGroup,
                Status = obj.Status,
                PermissionInSites = obj.PermissionInSites
            };
        }
        /// <summary>
        /// To the model.
        /// </summary>
        /// <param name="objs">The objs.</param>
        /// <returns></returns>
        public static List<SiteModel> ToModel(this List<SiteDto> objs)
        {
            var models = new List<SiteModel>();
            if (objs != null && objs.Count > 0)
                objs.ForEach(c => models.Add(c.ToModel()));
            return models;
        }
        #endregion
        
        #region Permission
        /// <summary>
        /// To the model.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <returns></returns>
        public static List<PermissionModel> ToModel(this IList<PermissionDto> list)
        {
            var models = new List<PermissionModel>();
            if (list != null)
            {
                models.AddRange(list.Select(item => item.ToModel()));
            }
            return models;
        }
        /// <summary>
        /// To the model.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <returns></returns>
        public static PermissionModel ToModel(this PermissionDto dto)
        {
            return new PermissionModel
            {
                Id = dto.Id,
                Title = dto.Title,
                Code = dto.Code,
                Description = dto.Description,
                Order = dto.Order,
                Status = dto.Status,
                ShowDefault = dto.ShowDefault,
                HasImage = dto.HasImage,
                MultiRow = dto.MultiRow
            };
        }
        #endregion

        #region PermissionInSite
        /// <summary>
        /// To the model.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <returns></returns>
        public static List<PermissionInSiteModel> ToModel(this List<PermissionInSiteDto> list)
        {
            var models = new List<PermissionInSiteModel>();
            if (list != null)
                list.ForEach(x => models.Add(x.ToModel()));
            return models;
        }
        /// <summary>
        /// To the model.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <returns></returns>
        public static PermissionInSiteModel ToModel(this PermissionInSiteDto dto)
        {
            return new PermissionInSiteModel
            {
                PermissionId = dto.PermissionId,
                SiteId = dto.SiteId
            };
        }
        /// <summary>
        /// To the business.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <returns></returns>
        public static List<PermissionInSite> ToBusiness(this List<PermissionInSiteDto> list)
        {
            var models = new List<PermissionInSite>();
            if (list != null)
                list.ForEach(x => models.Add(x.ToBusiness()));
            return models;
        }
        /// <summary>
        /// To the business.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <returns></returns>
        public static PermissionInSite ToBusiness(this PermissionInSiteDto dto)
        {
            return new PermissionInSite
            {
                PermissionId = dto.PermissionId,
                SiteId = dto.SiteId
            };
        }
        #endregion        
    }
}